//
//  LZCustomerCheckoutPresenter.m
//  laziz_Merchant
//
//  Created by xbn_iMac on 2016/12/21.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZCustomerCheckoutPresenter.h"
#import "NetworkServiceManager.h"
#import "NSString+Additions.h"
#import "LZUserModel.h"
#import "MJExtension.h"

#import "LZCustomerCheckoutModel.h"

extern NSString *merchant;
extern NSString *bound;

static NSString *const kGetOrderNoUrl = @"/order/getOrderNo";    // 结账--订单号生成接口接口
static NSString *const kOrderAddUrl = @"/order/add";    // 结账--生成订单接口

@implementation LZCustomerCheckoutPresenter

#pragma mark - 结账--订单号生成接口
/**
 结账--订单号生成接口
 
 @param success .
 @param fail .
 */
- (void)getOrderNoOnSuccess:(PresenterCommonSuccess)success onFail:(PresenterCommonFail)fail{
    
    NSString *urlStr = COMBINATION(merchant,kGetOrderNoUrl);
    
    [[NetworkServiceManager manager] executGetTask:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:fail]) {
            
            SAFE_BLOCK_INVOKE(success, resultData[@"data"]);
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(fail, error.localizedDescription);
    }];
}

#pragma mark - 结账--生成订单接口
/**
 结账--生成订单接口
 
 @param consumerId 结账客户ID
 @param orderId 订单号
 @param disCountId 用户优惠券ID或团购成团信息ID
 @param promotionType 优惠类型 1 优惠券 2 团购
 @param userDevice 使用优惠券用户的设备标识
 @param success .
 @param fail .
 */
- (void)productOrderWithConsumerId:(NSString*)consumerId andOrderId:(NSString*)orderId andDisCountId:(NSString*)disCountId andPromotionType:(NSString*)promotionType andUserDevice:(NSString*)userDevice onSuccess:(PresenterCommonSuccess)success onFail:(PresenterCommonFail)fail {
    
    if ([LZUserModel Instance].userId.length == 0) {
        return;
    }
    
    NSString *urlStr = COMBINATION(merchant,kOrderAddUrl);
    NSDictionary *params = @{@"sellerId":[LZUserModel Instance].sellerId,
                             @"token":[LZUserModel Instance].userToken,
                             @"userId":consumerId,
                             @"orderId":orderId,
                             @"disCountId":disCountId,
                             @"promotionType":promotionType,
                             @"userDevice":userDevice
                             };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:params success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:fail]) {
            
            LZCustomerCheckoutModel *model = [LZCustomerCheckoutModel mj_objectWithKeyValues:resultData[@"data"]];
            
            SAFE_BLOCK_INVOKE(success, model);
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(fail, error.localizedDescription);
    }];
    
}

@end
